<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    const new21Game = (N, K, W) => {
      // dp[i]：拿到分数 i 的概率
      const dp = new Array(N + 1).fill(0);
      // 没玩游戏时分数是0，所以拿0分的概率是1
      dp[0] = 1;
      for (let i = 1; i < N + 1; i++) {
        // 遍历区间 [i-W,i-1]/
        for (let j = i - W; j <= i - 1; j++) {
          // j>=0时，dp[j]才有意义; 分数i要<=K，所以j+1要<=K
          if (j >= 0 && j + 1 <= K) {
            // 对区间[i-W,i-1]的概率进行累加
            dp[i] += (1 / W) * dp[j];
          }
        }
      }
      let res = 0;
      // 分数落在[K,N]的概率 累加
      for (let i = K; i <= N; i++) {
        res += dp[i];
      }
      return res;
    };
    console.log(new21Game(n = 21, k = 17, maxPts = 10)) 
  </script>
</body>

</html>